package com.example.managemyself.ui.admin.dashboard;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.managemyself.R;
import com.example.managemyself.data.model.User;
import com.example.managemyself.util.UserManager;

import java.util.ArrayList;
import java.util.List;

/**
 * 管理员仪表板Fragment
 */
public class AdminDashboardFragment extends Fragment {
    
    private TextView tvWelcome;
    private RecyclerView rvDashboardCards;
    private DashboardCardAdapter cardAdapter;
    
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_admin_dashboard, container, false);
        
        initViews(view);
        setupDashboard();
        
        return view;
    }
    
    private void initViews(View view) {
        tvWelcome = view.findViewById(R.id.tv_welcome);
        rvDashboardCards = view.findViewById(R.id.rv_dashboard_cards);
        
        // 设置网格布局
        rvDashboardCards.setLayoutManager(new GridLayoutManager(getContext(), 2));
    }
    
    private void setupDashboard() {
        // 设置欢迎信息
        User currentUser = UserManager.getInstance().getCurrentUser();
        if (currentUser != null) {
            tvWelcome.setText("欢迎，" + currentUser.getDisplayName());
        }
        
        // 设置仪表板卡片
        List<DashboardCard> cards = createDashboardCards();
        cardAdapter = new DashboardCardAdapter(cards);
        rvDashboardCards.setAdapter(cardAdapter);
    }
    
    private List<DashboardCard> createDashboardCards() {
        List<DashboardCard> cards = new ArrayList<>();
        
        // 用户统计卡片
        cards.add(new DashboardCard(
                "用户统计",
                "总用户数: 156\n活跃用户: 89",
                R.drawable.ic_users,
                R.color.colorPrimary
        ));
        
        // 数据统计卡片
        cards.add(new DashboardCard(
                "数据统计",
                "番茄钟: 1,234\n健康记录: 567",
                R.drawable.ic_data,
                R.color.colorAccent
        ));
        
        // 系统状态卡片
        cards.add(new DashboardCard(
                "系统状态",
                "运行正常\n内存使用: 65%",
                R.drawable.ic_system,
                R.color.colorSuccess
        ));
        
        // 最近活动卡片
        cards.add(new DashboardCard(
                "最近活动",
                "新用户注册: 12\n今日登录: 45",
                R.drawable.ic_activity,
                R.color.colorWarning
        ));
        
        return cards;
    }
    
    /**
     * 仪表板卡片数据类
     */
    public static class DashboardCard {
        private String title;
        private String content;
        private int iconRes;
        private int colorRes;
        
        public DashboardCard(String title, String content, int iconRes, int colorRes) {
            this.title = title;
            this.content = content;
            this.iconRes = iconRes;
            this.colorRes = colorRes;
        }
        
        // Getters
        public String getTitle() { return title; }
        public String getContent() { return content; }
        public int getIconRes() { return iconRes; }
        public int getColorRes() { return colorRes; }
    }
} 